package models

import (
	"GoTime/response"
	"log"
	"time"

	"xorm.io/builder"
)

type Task struct {
	Id                        int       `xorm:"not null pk autoincr INT(11)"`
	StudentId                 int       `xorm:"index INT(11)"`
	SchoolId                  int       `xorm:"index INT(11)"`
	Subject                   string    `xorm:"comment('科目') index ENUM('1','2','3','4')"`
	Validetime                int64     `xorm:"INT(11)"`
	Result                    string    `xorm:"comment('0不合格1合格2审核中') ENUM('0','1','2')"`
	Needtime                  int64     `xorm:"default 0 comment('需要补充的学时') INT(11)"`
	Needmileage               float64   `xorm:"default 0 comment('需要补学公里数') FLOAT"`
	Reason                    string    `xorm:"default '' comment('不合格的原因') VARCHAR(255)"`
	Checktime                 time.Time `xorm:"index DATETIME"`
	Validmileage              float32   `xorm:"default 0 comment('有效公里数') FLOAT"`
	Isreport                  string    `xorm:"comment('0未上报1已上报') ENUM('0','1')"`
	PdfId                     int64     `xorm:"comment('电子培训部分记录表PDF文件ID') index INT(11)"`
	CheckStatus               string    `xorm:"comment('0不合格1合格') ENUM('0','1')"`
	RecheckStatus             string    `xorm:"comment('0不合格1合格') ENUM('0','1')"`
	NeedNetworktime           int       `xorm:"default 0 comment('需要远程的学时') INT(11)"`
	NeedClasstime             int       `xorm:"default 0 comment('需要课堂的学时') INT(11)"`
	NeedVehicletime           int       `xorm:"default 0 comment('需要实车的学时') INT(11)"`
	NeedSimulatortime         int       `xorm:"default 0 comment('需要模拟器的学时') INT(11)"`
	UpdateTime                time.Time `xorm:"index DATETIME"`
	Duration                  int64     `xorm:"not null comment('审核合格学时') INT(11)"`
	TotalInvalidTime          int64     `xorm:"not null comment('本培训部分本次的审核不合格总学时，n分钟') INT(11)" json:"total_invalid_time"`
	TransNeedFulTime          int       `xorm:"not null comment('从业需补总学时') default(0)" json:"trans_need_ful_time"`
	TransNeedFulvehcleTime    int       `xorm:"not null comment('从业需补实车学时') default(0)" json:"trans_need_fulvehcle_time"`
	TransNeedFulNetWorkTime   int       `xorm:"not null default(0) comment('从业需补远程学时')" json:"trans_need_ful_net_work_time"`
	TransNeedFulSimulatorTime int       `xorm:"not null default(0) comment('从业需补模拟学时')" json:"trans_need_ful_simulator_time"`
	TransNeedFulClassTime     int       `xorm:"not null default(0) comment('从业需补课堂学时')" json:"trans_need_ful_class_time"`
	IsCheck                   int       `xorm:"not null comment('是否上传考核表') INT(11)"`
	IsExam                    int       `xorm:"not null comment('是否考核合格') INT(11)"`
}

func (t *Task) StageRecord(schoolId string, filter builder.Cond, pageSize, offset int) (list []*response.StageListResponse, err error, count int64) {
	//川东北按着需补学时排序
	//if schoolId == "836" {
	//	err = engine.Table("dts_task").Alias("t").
	//		Select("t.id,ur.name as student_name,t.pdf_id,ur.id_type,ur.id_number,ur.phone,t.reason,t.check_status as chushen,st.istransfer,st.operation_status,t.recheck_status as fushen,t.subject,st.training_car_type as car_type,t.needtime,t.needmileage,t.isreport,st.id as student_id,t.need_simulatortime,t.need_vehicletime,t.need_networktime,t.need_classtime,t.checktime,t.validetime,t.validmileage,st.sign_in_time").
	//		Join("left", []string{"dts_student", "st"}, "t.student_id = st.id").
	//		Join("left", []string{"dts_user", "ur"}, "st.user_id = ur.id").
	//		Where("t.school_id = ? and st.isused = ?", schoolId, "1").
	//		And(filter).
	//		OrderBy("t.needtime").
	//		Limit(pageSize, offset).
	//		Find(&list)
	//} else {
	//	err = engine.Table("dts_task").Alias("t").
	//		Select("t.id,ur.name as student_name,t.is_exam,t.pdf_id,ur.id_type,ur.id_number,ur.phone,t.reason,t.is_check,t.check_status as chushen,st.istransfer,st.operation_status,t.recheck_status as fushen,t.subject,st.training_car_type as car_type,t.needtime,t.needmileage,t.isreport,st.id as student_id,t.need_simulatortime,t.need_vehicletime,t.need_networktime,t.need_classtime,t.checktime,t.validetime,t.validmileage,st.sign_in_time").
	//		Join("left", []string{"dts_student", "st"}, "t.student_id = st.id").
	//		Join("left", []string{"dts_user", "ur"}, "st.user_id = ur.id").
	//		Where("t.school_id = ? and st.isused = ?", schoolId, "1").
	//		And(filter).
	//		Desc("t.id").
	//		Limit(pageSize, offset).
	//		Find(&list)
	//}
	err = engine.Table("dts_task").Alias("t").
		Select("t.id,t.pdf_id,t.needtime,t.needmileage,t.isreport,t.check_status as chushen,t.recheck_status as fushen,t.checktime,t.reason,t.student_id,t.need_simulatortime,t.need_vehicletime,t.need_networktime,t.need_classtime,t.checktime,t.subject,student.training_car_type as car_type,student.sign_in_time,student.istransfer, user.name as student_name,user.id_type,user.id_number,user.phone, SUM(record.lasting_minute) as validetime,ROUND(SUM(record.mileage) / 10.0, 1) AS  validmileage").
		Join("LEFT", []string{"dts_student", "student"}, "t.student_id = student.id").
		Join("LEFT", []string{"dts_user", "user"}, "student.user_id = user.id").
		Join("LEFT", []string{"dts_learn_record", "record"}, "t.student_id = record.student_id AND t.subject = record.subject").
		Where("t.school_id = ?", schoolId).
		And(filter).
		GroupBy("t.id").
		Desc("t.id").
		Limit(pageSize, offset).
		Find(&list)
	count, err = engine.Table("dts_task").Alias("t").
		Select("COUNT(DISTINCT t.id) AS count").
		Join("LEFT", []string{"dts_student", "student"}, "t.student_id = student.id").
		Join("LEFT", []string{"dts_user", "user"}, "student.user_id = user.id").
		Join("LEFT", []string{"dts_learn_record", "record"}, "t.student_id = record.student_id AND t.subject = record.subject").
		Where("t.school_id = ?", schoolId).
		And(filter).
		Count()
	log.Println(err)
	return list, err, count
}
